#include <cstdio>
#include <vector>
using pir = std::pair<int, int>;

typedef long long ll;
const int N = 100005;
int n, q, u, k, cnt;
int e_cnt, heads[N], rt[N];
ll ans[N];

std::vector<pir> Q[N];

struct Node {
  int fa, siz, ch[2], val, tag;
  ll sum;
} tr[N << 2];

inline bool iden(const int &x) { return tr[tr[x].fa].ch[1] == x; }

inline void pushup(const int &x) {
  static int ls, rs;
  ls = tr[x].ch[0], rs = tr[x].ch[1];
  tr[x].sum = tr[x].val, tr[x].siz = 1;
  if (ls) tr[x].siz += tr[ls].siz, tr[x].sum += tr[ls].sum;
  if (rs) tr[x].siz += tr[rs].siz, tr[x].sum += tr[rs].sum;
}

inline void pushdown(const int &x) {
  static int ls, rs;
  ls = tr[x].ch[0], rs = tr[x].ch[1];
  if (ls)
    tr[ls].sum += 1ll * tr[x].tag * tr[ls].siz, tr[ls].tag += tr[x].tag,
        tr[ls].val += tr[x].tag;
  if (rs)
    tr[rs].sum += 1ll * tr[x].tag * tr[rs].siz, tr[rs].tag += tr[x].tag,
        tr[rs].val += tr[x].tag;
  tr[x].tag = 0;
}

inline void pushall(const int &x) {
  if (tr[x].fa) pushall(tr[x].fa);
  pushdown(x);
}

inline void connect(const int &x, const int &fa, const bool &typ) {
  if (x) tr[x].fa = fa;
  if (fa) tr[fa].ch[typ] = x;
}

inline void rotate(const int &x) {
  bool xtofa = iden(x);
  int fa = tr[x].fa, ffa = tr[fa].fa;
  connect(tr[x].ch[!xtofa], fa, xtofa);
  connect(x, ffa, iden(fa));
  connect(fa, x, !xtofa);
  pushup(fa), pushup(x);
}

inline void splay(const int &x, const int &tar, const int &rtid) {
  for (int fa = tr[x].fa; (fa = tr[x].fa) != tar; rotate(x))
    if (tr[fa].fa != tar) rotate(iden(x) == iden(fa) ? fa : x);
  if (!tar) rt[rtid] = x;
}

inline void insert(const int &v, const int &rtid) {
  int x = rt[rtid], fa = 0;
  if (!x) {
    rt[rtid] = x = ++cnt;
    tr[x].val = v, tr[x].fa = fa, tr[x].siz = 1, tr[x].sum = v, tr[x].tag = 0;
    return;
  }
  while (true) {
    pushdown(x);
    fa = x, x = tr[x].ch[v > tr[x].val];
    if (!x) {
      tr[fa].ch[v > tr[fa].val] = ++cnt;
      tr[cnt].val = v, tr[cnt].fa = fa, tr[cnt].siz = 1, tr[cnt].sum = v, tr[cnt].tag = 0;
      pushup(fa);
      splay(cnt, 0, rtid);
      return;
    }
  }
}

void solve(std::vector<int> &vec, int x) {
  if (!x) return;
  pushdown(x);
  solve(vec, tr[x].ch[0]);
  vec.push_back(tr[x].val);
  solve(vec, tr[x].ch[1]);
}

inline void merge(const int &rtx, const int &rty) {
  if (tr[rt[rtx]].siz < tr[rt[rty]].siz) std::swap(rt[rtx], rt[rty]);
  std::vector<int> vec;
  solve(vec, rt[rty]);
  for (auto &i : vec) insert(i, rtx);
}

inline ll query(int k, const int &rtid) {
  int x = rt[rtid], pos;
  while (x) {
    pushdown(x);
    if (tr[x].val < k) pos = x, x = tr[x].ch[1];
    else
      x = tr[x].ch[0];
  }
  if (!pos) return tr[pos].sum;
  splay(pos, 0, rtid);
  if (!tr[pos].ch[1]) return 0;
  else
    return tr[tr[pos].ch[1]].sum;
}

inline void add(const int &v, const int &rtid) {
  static int RT;
  RT = rt[rtid], tr[RT].tag += v, tr[RT].sum += 1ll * v * tr[RT].siz, tr[RT].val += v;
}

struct Edge {
  int v, nxt, w;
} e[N];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

void dfs(int u) {
  insert(0, u);
  for (int i = heads[u], v; i; i = e[i].nxt) {
    dfs(v = e[i].v);
    add(e[i].w, v);
    merge(u, v);
  }
  for (auto &i : Q[u]) { ans[i.first] = query(i.second, u); }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("forging.in", "r", stdin);
  freopen("forging.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  for (int i = 2, fa, w; i <= n; ++i) {
    scanf("%d%d", &fa, &w);
    add(fa, i, w);
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &u, &k);
    Q[u].push_back(std::make_pair(i, k));
  }
  dfs(1);
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}